package com.example.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.PaySystemConstant;
import com.example.config.exception.GlobalExcetion;
import com.example.mapper.UserPayRecordOutMapper;
import com.example.pay.domain.UserPayRecordOut;

import com.example.service.UserPayRecordOutService;
import com.example.system.domain.BusSystemTableUser;
import com.example.system.service.BusSystemTableService;
import com.example.system.service.BusSystemTableUserService;
import com.example.util.sql.DBSql;
import com.example.util.sql.SqlGenerator;
import com.example.util.sql.SqlResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.sql.DataSource;
import java.util.Date;

/**
* @author CETC
* @description 针对表【user_pay_record_out_1】的数据库操作Service实现
* @createDate 2025-11-01 22:17:31
*/
@Service
public class UserPayRecordOutServiceImpl extends ServiceImpl<UserPayRecordOutMapper, UserPayRecordOut>
    implements UserPayRecordOutService {

    @Autowired
    private DataSource dataSource;

    @Autowired
    private BusSystemTableService busSystemTableService;

    @Autowired
    private BusSystemTableUserService busSystemTableUserService;

    @Override
    public UserPayRecordOut getOneById(String id) throws GlobalExcetion {
        String tableName=busSystemTableService.getTableNameByUUID(id);
        return this.baseMapper.getById(tableName, id);
    }

    @Override
    public UserPayRecordOut saveUserPayRecordOut(String userId, UserPayRecordOut userPayRecordOut) throws GlobalExcetion {
        if(userPayRecordOut==null){
            throw new GlobalExcetion(1,"传入的数据不得为空");
        }
        BusSystemTableUser tableUser=busSystemTableUserService.selectByUserId(userId, PaySystemConstant.User_Pay_RecordOut_tableName);
        String tableName=tableUser.getSaveTableName();
        String uuid=busSystemTableService.createUUID(tableName);
        userPayRecordOut.setUuid(uuid);
        userPayRecordOut.setPayDate(new Date());
        userPayRecordOut.setCreateDate(new Date());
        DBSql dbSql=new DBSql(dataSource);
        SqlResult sqlResult=SqlGenerator.generateInsert(tableName, userPayRecordOut);
        dbSql.executeInsert(sqlResult.getSql(),sqlResult.getParams().toArray());
        return userPayRecordOut;
    }

    @Override
    public UserPayRecordOut updateUserPayRecordOutById(UserPayRecordOut userPayRecordOut) throws GlobalExcetion {
        if(userPayRecordOut==null || userPayRecordOut.getUuid()==null){
            throw new GlobalExcetion(1,"非法数据");
        }
        String tableName=busSystemTableService.getTableNameByUUID(userPayRecordOut.getUuid());
        DBSql dbSql=new DBSql(dataSource);
        SqlResult sqlResult=SqlGenerator.generateUpdateById(tableName,userPayRecordOut,"uuid");
        dbSql.executeUpdate(sqlResult.getSql(),sqlResult.getParams().toArray());
        return userPayRecordOut;
    }

    @Override
    public IPage<UserPayRecordOut> page(Page<UserPayRecordOut> page, String userId) throws GlobalExcetion {
        BusSystemTableUser tableUser=busSystemTableUserService.selectByUserId(userId, PaySystemConstant.User_Pay_RecordOut_tableName);
        String tableName=tableUser.generateUnionAllTable("from_uuid");
        return this.baseMapper.page(page,tableName,userId);
    }
}




